package com.waimai.app.qa.data;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.jsefa.Deserializer;
import org.jsefa.csv.CsvIOFactory;
import org.jsefa.csv.config.CsvConfiguration;

/**
 * @author AiHui
 */
public class CsvOperate {
	public HashMap<String, Object> deserialize(String inFileName, String encoding) {
		CsvConfiguration config = new CsvConfiguration();
		config.setFieldDelimiter('|');
		
		Deserializer d = CsvIOFactory.createFactory(config, new Class[] {Item.class}).createDeserializer();

		InputStreamReader reader = null;
		try {
			reader = new InputStreamReader(new FileInputStream(inFileName), encoding);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		d.open(reader);
		HashMap<String, Object> es = new HashMap<String, Object>();
		while (d.hasNext()) {
			Item e = d.next();
			if(e.elementName.contains("#")){
				continue;
			}
			es.put(e.elementName, e);
		}
		try {
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		d.close(true);
		return es;
	}
}
